package admin_registros;

import conexion.AdminConexion;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelbd.TProfesor;

/**
 *
 * @author El APRENDIZ www.elaprendiz.net63.net
 */
public class AdminRegistros {
    private static ResultSet rs = null;
    private static Statement st = null;
    
    private static ResultSet rs2 = null;
    private static Statement st2 = null;
    
    private static Connection cn = null;
    
    private final int CXN = 0;
    private final int CXN2 = 1;
    
    
    private AdminRegistros()
    {
        abrirConexion();
    }    
    public static AdminRegistros getInstance()
    {
        return InstanciaAdminRegistros.ar;
    }    
    private static class InstanciaAdminRegistros{
        private static final AdminRegistros ar = new AdminRegistros();
     }
    
    private void abrirConexion()
    {
        cn = AdminConexion.init_conexion();
        if(cn != null)
        {
            try {
                st = cn.createStatement();
            } catch (SQLException ex) {
                Logger.getLogger(AdminRegistros.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            System.out.println("Error: no se pudo conectar a la base de datos");
        }
    }
    
    private void abrirConexion2()
    {
        try{
            if(cn.isClosed())
            {
                cn = AdminConexion.init_conexion();
                
                st2 = cn.createStatement();
            }
        }catch(SQLException ex){}
    }
    
    private void conectarse(int cxn)
    {
        try{
            if(cn.isClosed() && cxn == this.CXN )
                abrirConexion();
            else if(cxn == this.CXN2)
                abrirConexion2();
        }catch(SQLException ex){ex.getMessage();}
    }
    
    private void cerrarConexion(int opcion)
    {
        try{
            if(opcion == this.CXN)
            {
                rs.close();
                st.close();
                cn.close();
            }else if(opcion == this.CXN2)
            {
                rs2.close();
                st2.close();
            }
        }catch(SQLException ex){}
        
    }
    
    public Vector getProfesores()
    {    
        Vector datos = new Vector();
        conectarse(this.CXN);
        try
        {
            rs = st.executeQuery("select * from profesor");          
            while(rs.next())
            {
                datos.add(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+
                        rs.getString(4)+" "+rs.getString(5)+" "+rs.getString(6));
            }
        }catch(SQLException ex){
        Logger.getLogger(AdminRegistros.class.getName()).log(Level.SEVERE, null, ex);
        }
        cerrarConexion(this.CXN);        
        return datos;
    }
    
    public ArrayList<TProfesor> getListaProfesores()
    {
        ArrayList<TProfesor> profes = new ArrayList<TProfesor>();
        TProfesor prof;
        conectarse(this.CXN);
        try{
            rs = st.executeQuery("select * from profesor");
            while(rs.next())
            {
                prof = new TProfesor(rs.getInt(1),rs.getString(2),rs.getString(3),
                        rs.getString(4),rs.getString(5),rs.getString(6));
                profes.add(prof);                
            }
        }catch(SQLException ex){}
        cerrarConexion(this.CXN);        
        return profes;
    }
}
